package cn.yuanqiao.activiti.listener;

import cn.yuanqiao.activiti.domain.ActArchiveData;
import cn.yuanqiao.activiti.service.IActArchiveDataService;
import cn.yuanqiao.common.exception.activiti.ArchiveProcessException;
import cn.yuanqiao.common.utils.DateUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WritsArchiveBorrowListener
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2023/6/5 19:24
 * @Blog: huahuacoco.top
 */

@Component("ArchiveStartListener")
public class ArchiveStartListener implements ExecutionListener {

    @Autowired
    private IActArchiveDataService actArchiveDataService;


    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        Object obj = execution.getVariable("archiveData");
        Map<String, Object> variables = execution.getVariables();
        DelegateExecution parent = execution.getParent();
        String superExecutionId = execution.getSuperExecutionId();
        String rootProcessInstanceId = execution.getRootProcessInstanceId();
        System.out.println(1111);

        if (obj == null){
            throw new ArchiveProcessException("档案数据不能为空");
        }
        List<Map<String, Object>> archiveData = ( List<Map<String, Object>> )obj;

        System.out.println("启动流程监听类，维护actArchiveData表");

        List<ActArchiveData> archiveDataList = new ArrayList<>();

        for (Map<String, Object> item:archiveData){
            ActArchiveData actArchiveData = new ActArchiveData();
            actArchiveData.setActivitiKey(execution.getProcessInstanceBusinessKey());
            actArchiveData.setDeployId(execution.getId());
            actArchiveData.setArchiveId(item.get("ID").toString());
            actArchiveData.setCreateTime(DateUtils.getNowDate());
            actArchiveData.setSTATE("1");
            archiveDataList.add(actArchiveData);
        }

        //actArchiveDataService.batchInsertActArchiveData(archiveDataList);

    }
}
